package top.bug.blog.schedule.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;
import top.bug.blog.annotation.ScheduledJob;
import top.bug.blog.service.OperationLogService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 日志清理 定时任务
 */
@ScheduledJob(
        jobName = "LogCleanJob",
        cronExpression = "0 0 2 * * ?",
        description = "日志清理任务"
)
@Component
@Slf4j
public class LogCleanJob implements Job {
    @Resource
    private OperationLogService operationLogService;

    /**
     * 每天凌晨2点执行清理----后台操作日志
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("===========【后台操作日志清理任务】===========");
        LocalDateTime threshold = LocalDateTime.now().minusDays(10);
        int deletedCount = operationLogService.deleteBeforeDate(threshold);
        log.info("删除 {} 条 {} 前的日志", deletedCount, threshold);
    }
}